package com.li.chat.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.li.chat.common.BaseResponse;
import com.li.chat.common.ErrorCode;
import com.li.chat.common.ResultUtils;
import com.li.chat.constant.EmailConstant;
import com.li.chat.exception.BusinessException;
import com.li.chat.exception.ThrowUtils;
import com.li.chat.model.entity.User;
import com.li.chat.service.UserService;
import org.checkerframework.checker.units.qual.A;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static com.li.chat.exception.ThrowUtils.throwIf;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JavaMailSender mailSender;//注入QQ发送邮件的bean
    @Autowired
    private RedissonClient redissonClient;

    //用户登录
    @PostMapping("/register")
    public SaResult register(@RequestBody User user) {
        User re=userService.getById(user.getUser_id());
        if(!re.getPassword().equals(user.getPassword())){

        }
        StpUtil.login(user.getUser_id());
        //返回token
        SaResult.ok().setData(StpUtil.getTokenValue());

        return SaResult.ok().setData(StpUtil.getTokenInfo());
    }

    //用户注册
    @PostMapping("/add")
    public String addUser(@RequestBody User user,@RequestParam String code) {
        RMap<String,String> Rmap=redissonClient.getMap("user:"+user.getUser_id());

        if(userService.oneById(user.getUser_id())!=null){
            return "账号重复";
        }
        if(code==null){
            return "验证码未输入";
        }
        if(!code.equals(Rmap.get(user.getUser_id().toString()))){
            return "验证码或账号不正确";
        }
        userService.save(user);
        return "注册成功";
    }
    //邮箱注册,生成代注册表
    @PostMapping("/getcode")
    public BaseResponse<String> GetVlCode (@RequestBody JSONObject email) {
        String toemail=email.get("user_email").toString();
        String id=email.get("user_id").toString();
        //生成随机验证码
        Random random = new Random();
        Integer randomNumber = 1000 + random.nextInt(9000);

            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setSubject("树洞：验证码");
            simpleMailMessage.setText("验证码："+randomNumber);
            simpleMailMessage.setTo(toemail);
            simpleMailMessage.setFrom(EmailConstant.EMAIL_SENDER);

        try{
            mailSender.send(simpleMailMessage);
            RMap<String,String> rmap = redissonClient.getMap("user:"+id);
            rmap.put(id,randomNumber.toString());
            rmap.expire(10, TimeUnit.HOURS);
        }catch (RuntimeException e){
            throw new BusinessException(ErrorCode.EMAIL_FAIL);
        }

        return ResultUtils.success("发送成功，请等待接收");
    }
}
